<?php

namespace app\api\controller;

use app\api\model\MemberModel;
use think\facade\Cache;
use think\facade\Request;

class AuthController extends ApiController {
    protected $mid;

    protected function _initialize() {
        header('Access-Control-Allow-Origin:*');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        header('Access-Control-Allow-Headers:x-requested-with,content-type');  //响应头 请按照自己需求添加。
        $str = urldecode($this->request->get('token', 0));
        if (empty($str)) {
            $str = Request::header('Authorization');
        }

        if (env('app_debug') == 1) {
            $userInfo = MemberModel::where('account', $str)->find();

            if($userInfo) {
                $this->_userInfo = [
                    'account'   => $userInfo['account'],
                    'id'        => $userInfo['id'],
                    'avatar'    => $userInfo['avatar'],
                    'nickname'  => $userInfo['nickname'],
                    'phone'     => $userInfo['phone'],
                    'parent_id' => $userInfo['parent_id'],
                    'level'     => $userInfo['level'],
                    'status'    => $userInfo['status'],
                    'credit1'   => $userInfo['credit1'],
                    'credit2'   => $userInfo['credit2']
                ];
                $this->mid = $userInfo['id'];
            }else{
                if ($this->_check_login($str)) {
                    if (!$this->_userInfo['status']) $this->_loginFail('账号已被锁定,无法登陆!');
                    $this->mid = $this->_userInfo['id'];
                } else {
                    $this->_loginFail('无效的登录!');
                }
            }
        }else {
            if ($this->_check_login($str)) {
                if (!$this->_userInfo['status']) $this->_loginFail('账号已被锁定,无法登陆!');
                $this->mid = $this->_userInfo['id'];
            } else {
                $this->_loginFail('无效的登录!');
            }
        }
    }

    protected function _check_login($str) {
        if (empty($str)) return false;
        $req = (array)json_decode(base64_decode($str));
        if (empty($req) || empty($req['i']) || empty($req['t'])) return false;
        $id = $req['i'];
        $token = html_entity_decode($req['t']);
        $userInfo = MemberModel::where('id', $id)->find();

        if (empty($userInfo)) return false;
        $this->_userInfo = $userInfo->toArray();
        $account = $this->_userInfo['account'];

        $uk = Cache::get(md5('meet_you' . $account));

        if (password_verify($uk[0] . $uk[1] . $account . $uk[2], $token)) {
            if (!MemberModel::be(['account' => $account])) return false;

            $userInfo = MemberModel::where('account', $account)->find();

            $this->_userInfo = [
                'account'   => $userInfo['account'],
                'id'        => $userInfo['id'],
                'avatar'    => $userInfo['avatar'],
                'nickname'  => $userInfo['nickname'],
                'phone'     => $userInfo['phone'],
                'parent_id' => $userInfo['parent_id'],
                'level'     => $userInfo['level'],
                'status'    => $userInfo['status'],
                'credit1'   => $userInfo['credit1'],
                'credit2'   => $userInfo['credit2']
            ];

            $this->_token = base64_encode(json_encode(['i' => $userInfo['id'], 't' => password_hash($uk[0] . $uk[1] . $account . $uk[2], PASSWORD_DEFAULT)]));
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取会员等级
     * @param $level
     */
    public function getMemberLevel($level) {
        $level = db('meetyou_member_level')->where('status', 1)->where('level', $this->_userInfo['level'])->find();
        if (empty($level)) {
            $level = db('meetyou_member_level')->where('status', 1)->order('level', 'asc')->find();
        }
        return $level;
    }

    /**
 * 获取当前会员积分
 */
    public function getCredit1($id = 0) {
        if (empty($id)) $id = $this->_userInfo['id'];
        return round(db('meetyou_member')->where('id', $id)->value('credit1'), 2);
    }


    /**
     * 获取当前会员余额
     */
    public function getCredit2($id = 0) {
        if (empty($id)) $id = $this->_userInfo['id'];
        return round(db('meetyou_member')->where('id', $id)->value('credit2'), 2);
    }



}